<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll"
          >批量删除</el-button
        >
        <el-input
          v-model="select_word"
          size="mini"
          placeholder="请输入单位名称"
          class="handle-input"
        ></el-input>
        <el-button
          type="primary"
          size="mini"
          @click="centerDialogVisible = true"
          >添加组织信息</el-button
        >
      </div>
    </div>
    <!-- 查看 -->
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="570px"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column
        prop="principal"
        label="负责人"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="zucunTaskForce"
        label="驻村工作队"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column label="成员管理" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="memberEdit(scope.row.id, scope.row.zucunTaskForce)"
            >成员管理</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="unit"
        label="来自单位"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <!-- 添加 -->
    <el-dialog
      title="添加组织管理信息"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form :model="registerForm" ref="registerForm" label-width="80px">
        <el-form-item prop="principal" label="负责人" size="mini">
          <el-input
            v-model="registerForm.principal"
            placeholder="负责人"
          ></el-input>
        </el-form-item>
        <el-form-item prop="zucunTaskForce" label="驻村工作队" size="mini">
          <el-input
            v-model="registerForm.zucunTaskForce"
            placeholder="驻村工作队"
          ></el-input>
        </el-form-item>
        <el-form-item prop="unit" label="来自单位" size="mini">
          <el-input
            v-model="registerForm.unit"
            placeholder="来自单位"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="centerDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" @click="addOrganization">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改组织管理信息"
      :visible.sync="editVisible"
      width="400px"
      center
    >
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="principal" label="负责人" size="mini">
          <el-input v-model="form.principal" placeholder="负责人"></el-input>
        </el-form-item>
        <el-form-item prop="zucunTaskForce" label="驻村工作队" size="mini">
          <el-input
            v-model="form.zucunTaskForce"
            placeholder="驻村工作队"
          ></el-input>
        </el-form-item>
        <el-form-item prop="unit" label="来自单位" size="mini">
          <el-input v-model="form.unit" placeholder="来自单位"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editVisible = false">取消</el-button>
        <el-button size="mini" @click="editOrganization">确定</el-button>
      </span>
    </el-dialog>

    <!-- 删除 -->
    <el-dialog
      title="删除组织管理信息"
      :visible.sync="delVisible"
      width="300px"
      center
    >
      <div align="center">删除不可恢复，是否确定删除？</div>
      <span slot="footer">
        <el-button size="mini" @click="delVisible = false">取消</el-button>
        <el-button size="mini" @click="deleteRow">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  setOrganization,
  getAllOrganization,
  updateOrganization,
  deleteOrganization,
} from "../api/index";
import { mixin } from "../mixins/index";
export default {
  mixins: [mixin],
  data() {
    return {
      centerDialogVisible: false, //添加弹窗是否显示
      editVisible: false, //编辑弹窗是否显示
      delVisible: false, //删除弹窗是否显示
      tableData: [],
      tempData: [],
      select_word: "",
      pageSize: 13, //分页每页大小
      currentPage: 1, //当前页
      idx: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
      // 添加框
      registerForm: {
        principal: "",
        zucunTaskForce: "",
        unit: "",
      },
      // 编辑框
      form: {
        id: "",
        principal: "",
        zucunTaskForce: "",
        unit: "",
      },
    };
  },
  computed: {
    //计算当前搜索结果表里的数据
    data() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  watch: {
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function () {
      if (this.select_word === "") {
        this.tableData = this.tempData;
      } else {
        this.tableData = [];
        for (let item of this.tempData) {
          if (item.unit.includes(this.select_word)) {
            this.tableData.push(item);
          }
        }
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    // 查询所有组织信息
    getData() {
      this.tempData = [];
      this.tableData = [];
      getAllOrganization().then((res) => {
        this.tempData = res;
        this.tableData = res;
        this.currentPage = 1;
      });
    },
    // 添加组织信息
    addOrganization() {
      let params = new URLSearchParams();
      params.append("principal", this.registerForm.principal);
      params.append("zucunTaskForce", this.registerForm.zucunTaskForce);
      params.append("unit", this.registerForm.unit);

      setOrganization(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("添加成功", "success");
          } else {
            this.notify("添加失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.centerDialogVisible = false;
    },
    // 弹出编辑页面
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        principal: row.principal,
        zucunTaskForce: row.zucunTaskForce,
        unit: row.unit,
      };
    },
    // 保存编辑页面修改的数据
    editOrganization() {
      let params = new URLSearchParams();
      params.append("id", this.form.id);
      params.append("principal", this.form.principal);
      params.append("zucunTaskForce", this.form.zucunTaskForce);
      params.append("unit", this.form.unit);

      updateOrganization(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("修改成功", "success");
          } else {
            this.notify("修改失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.editVisible = false;
    },
    // 删除组织管理信息
    deleteRow() {
      deleteOrganization(this.idx)
        .then((res) => {
          if (res) {
            this.getData();
            this.notify("删除成功", "success");
          } else {
            this.notify("删除失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.delVisible = false;
    },
    // 转向成员管理页面
    memberEdit(id, zucunTaskForce) {
      this.$router.push({ path: `/Member`, query: { id, zucunTaskForce } });
    },
  },
};
</script>
<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.pagination {
  display: flex;
  justify-content: center;
}
</style>



